home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Practical Algorithms for Image Analysis
/
Practical Algorithms for Image Analysis.iso
/
TARFILE.GZ
/
tarfile
/
ch_3.6
/
subsample
/
smoothr.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-09-11
|
2KB
|
68 lines
/*
* smoothr.c
*
* Practical Algorithms for Image Analysis
*
* Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
*/
/* SMOOTHR: function performs 2-D smoothing by separable, 1-D averaging
* and returns subsampled image
* usage: smoothr (imgI, imgO, nFltr, rSubsample)
*/
#include <images.h>
#include <tiffimage.h>
void
smoothr (imgI, imgO, nFltr, rSubsample)
Image *imgI, *imgO; /* input,output image structures */
long nFltr; /* number of filter coefficients */
long rSubsample; /* subsample rate */
{
Image *imgT; /* intermediate image structure */
unsigned char **imgIn, /* input image */
**imgTi, /* intermediate image */
**imgOut; /* output image */
long width, height; /* size of image */
long sum; /* sum of filter convolution at a pixel */
long midFltr; /* middle coefficient index of filter */
long xEnd, yEnd; /* end coefficients of convolution */
long xOut, yOut; /* output image coordinates */
long x, y, i;
/* initialize images */
imgIn = ImageGetPtr (imgI); /* input image */
height = ImageGetHeight (imgI);
width = ImageGetWidth (imgI);
imgT = ImageAlloc (height, width, 8); /* intermediate image */
imgTi = ImageGetPtr (imgT);
imgOut = ImageGetPtr (imgO); /* output image */
/* perform row-wise smoothing */
midFltr = (nFltr - 1) / 2;
xEnd = width - midFltr;
for (y = 0; y < height; y++) {
for (x = midFltr; x < xEnd; x += rSubsample) {
sum = imgIn[y][x];
for (i = 1; i <= midFltr; i++)
sum += imgIn[y][x - i] + imgIn[y][x + i];
imgTi[y][x] = (unsigned char) (sum / nFltr);
}
}
/* perform column-wise convolution */
yEnd = height - midFltr;
for (y = midFltr, yOut = 0; y < yEnd; y += rSubsample) {
for (x = midFltr, xOut = 0; x < xEnd; x += rSubsample) {
sum = imgTi[y][x];
for (i = 1; i <= midFltr; i++)
sum += imgTi[y - i][x] + imgTi[y + i][x];
imgOut[yOut][xOut++] = (unsigned char) (sum / nFltr);
}
yOut++;
}
return;
}